<template>
  <div class="school_infs">
    <Card>
      <p slot="title">
        <Icon type="navicon-round"></Icon>
        教师请假
      </p>
      <Row type="flex" justify="space-between">
        <div>
          教师名称：
          <Input v-model="searchparam.teacherName" placeholder="教师姓名关键字" @on-change="queryFn"
                 style="width: 100px;margin-right: 4px;"/>
          <div class="db_inline">
            状态：
            <Select v-model="searchparam.status" style="width:80px" @on-change="queryFn">
              <Option v-for="item in statusList" :value="item.id" :key="item.id">{{ item.name }}</Option>
            </Select>
          </div>
          <div class="db_inline">
            申请时间：
            <DatePicker type="date" placeholder="开始时间" v-model="searchparam.bTime" @on-change="searchparam.bTime=$event"
                        style="width: 110px"></DatePicker>
            至
            <DatePicker type="date" placeholder="结束时间" v-model="searchparam.eTime" @on-change="searchparam.eTime=$event"
                        style="width: 110px"></DatePicker>
          </div>
          <span @click="queryFn">
              			<Button type="info"><Icon type="search"></Icon>搜索</Button>
        			</span>
          <span @click="restFn">
              			<Button type="ghost"><Icon type="refresh"></Icon>刷新</Button>
        			</span>
          <Button @click="exportFn" type="info">
            <Icon type="navicon" size="20"></Icon>
            <span>全部导出</span>
          </Button>
        </div>
      </Row>
      <Row class="margin-top-10">
        <Table :height='tableHeight' ref="backgroundTable" :columns="dataColumns" :data="dataTable"></Table>
      </Row>
      <Row type="flex" justify="end" class="footer">
        <div class="margin-top-10">
          <Page :total="searchparam.total" :current="searchparam.current" :page-size="searchparam.size"
                @on-change="pageChange" show-elevator></Page>
        </div>
      </Row>
    </Card>

    <Modal v-model="details.visible" :title="details.title" width="450" v-on:on-ok="AgreeBtn"
           v-on:on-cancel="DisagreeBtn"
           :mask-closable="false">
      <div style="line-height: 30px;font-size: 16px;height: 250px">
        <div>
          <Row>
            <div style="height: 80px">
              &nbsp;<strong>&nbsp;原因：</strong>{{ details.form.reason }}
            </div>
            <div style="margin-top: 15px">
              &nbsp;<strong>审批：</strong>
              <RadioGroup v-model="details.form.status">
                <Radio label='1' :disabled="!details.judge">同意</Radio>
                <Radio label='2' :disabled="!details.judge">不同意</Radio>
              </RadioGroup>
            </div>
            <div style="margin-top: 10px">
              <strong> <label style="color: red">*</label> 意见：</strong>
              <Input v-model="details.form.judeContent" :readonly="!details.judge" type="textarea" :rows="3"
                     :autosize="{minRows: 3,maxRows: 5}"
                     placeholder="审批意见" style="width:80%;position: relative;top: 25px"></Input>
            </div>
          </Row>
        </div>
      </div>
      <div slot="footer" v-if="details.judge">
        <Button type="text" @click="DisagreeBtn">关闭</Button>
        <Button type="primary" @click="AgreeBtn" :loading="details.loading">审批</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import API from "@/api/api";
import VmBtable from "@/components/vm/vm-btable";
import vueUpload from "@/components/views/upload/Upload";
import Cookie from "js-cookie"

export default {
  name: "Background",
  components: {
    VmBtable,
    vueUpload,
  },
  data() {
    return {
      cutHeight: 280,
      tableHeight: 640,
      statusList: [
        {
          id: '0',
          name: '未审批'
        }, {
          id: '1',
          name: '同意'
        },
        {
          id: '2',
          name: '不同意'
        },

      ],
      details: {
        title: '',
        visible: false,
        loading: false,
        judge: true,
        form: {
          id: '',
          reason: '',
          status: 0,
          judeContent: ''
        }
      },
      readList: [{
        id: ' ',
        name: '全部'
      },
        {
          id: '已读',
          name: '已读'
        },
        {
          id: '未读',
          name: '未读'
        },
      ],
      searchTitle: '',
      loginType: Cookie.getJSON('loginType'),
      searchparam: {
        current: 1,
        size: 20,
        teacherName: '',
        status: '',
        eTime: '',
        bTime: '',
      },
      dataColumns: [{
        type: 'index',
        title: '序号',
        align: 'center',
        minWidth: 60
      },
        {
          title: '教师名',
          key: 'teacherName',
          minWidth: 120,
          align: 'center'
        },
        {
          title: '请假类型',
          key: 'leaveType',
          minWidth: 120,
          align: 'center'
        },
        {
          title: '开始时间',
          key: 'startTime',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '结束时间',
          key: 'endTime',
          minWidth: 150,
          align: 'center',
        },
        {
          title: '申请时间',
          key: 'createTime',
          minWidth: 150,
          align: 'center',
        },
        {
          title: '状态',
          key: 'status',
          minWidth: 150,
          align: 'center',
          render: (h, param) => {
            let status = param.row.status;
            let str = '未审核';
            if (status === 1) {
              str = '已通过';
            } else if (status === 2) {
              str = '不通过';
            }
            return h('div', {}, str);
          }
        },
        {
          title: '审批时间',
          key: 'judeTime',
          minWidth: 150,
          align: 'center',
        },
        {
          title: '操作',
          align: 'center',
          minWidth: 160,
          render: (h, params) => {
            let btn = [];
            let tip = '';
            if (params.row.status !== '0') {
              tip = '详情'
            } else {
              tip = '审核'
            }
            let detailBtn = h('Button', {
              props: {
                type: 'info',
                size: 'small'
              },
              style: {
                marginRight: '5px'
              },
              on: {
                click: () => {
                  this.detailFn(params.row)
                }
              }
            }, tip);

            btn.push(detailBtn);
            return h('div', btn);
          }
        }
      ],
      dataTable: [],
    }
  },
  methods: {
    exportFn() {
      this.$Modal.confirm({
        title: '提示',
        content: '是否导出全部老师请假报告？',
        onOk: () => {
          API.fetchTeacherLeaveexport(this.searchparam);
        },
        onCancel: () => {

        }
      })
    },
    queryFn() {
      API.fetchTeacherLeave(this.searchparam).then(resp => {
        this.dataTable = resp.data.records;
        this.searchparam.total = resp.data.total;
        this.searchparam.current = resp.data.current;
      })
    },
    pageChange(val) {
      this.searchparam.current = val;
      this.queryFn();
    },
    restFn() {
      this.searchparam.status = '';
      this.searchparam.teacherName = '';
      this.searchparam.bTime = '';
      this.searchparam.eTime = '';
      this.queryFn();
    },
    detailFn(data) {
      this.details.visible = true;
      Object.assign(this.details, {
        visible: true,
        title: data.teacherName + '的请假',
      });
      Object.assign(this.details.form, data);
      if (data.status === 0) {
        this.details.judge = true;
        this.details.form.status = '1'
      } else {
        this.details.form.status = data.status + '';
        this.details.judge = false;
      }
    },
    AgreeBtn() {
      if (!this.details.judge) {
        this.complete();
        return;
      }
      if (!this.details.form.judeContent) {
        this.$Message.warning('请输入审批意见！');
        return;
      }
      this.details.loading = true;
      API.judgeTeacherLeave(this.details.form).then(res => {
        this.complete();
        this.queryFn();
      })
    },
    DisagreeBtn() {
      this.complete();
    },
    cancelFn() {
    },
    complete() {
      Object.assign(this.details, {
        visible: false,
        title: '',
        loading: false,
        form: {}
      })
    },
    submitFn() {
    },
    setHeight() {
      this.tableHeight = window.innerHeight - this.$refs.backgroundTable.$el.offsetTop - this.cutHeight;
    }
  },
  mounted() {
    this.queryFn();
    this.setHeight();
  }
}
</script>

<style scoped>

</style>
